package org.tmad.ocms.web;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import org.tmad.ocms.log.LogFactory;
import org.tmad.ocms.model.Volunteer;
import org.tmad.ocms.service.MasterDataService;
import org.tmad.ocms.service.RegistrationService;

/**
 * Servlet implementation class RegistrationServlet
 */
public class RegistrationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger = LogFactory.getLogger();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RegistrationServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("states", MasterDataService.getStates());
		request.setAttribute("caseTypes", MasterDataService.getCaseCategories());
		request.setAttribute("helpTypes", MasterDataService.getHelpCategories());
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher("/register.jsp");
		dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String firstName = request.getParameter("firstname");
		String lastName = request.getParameter("lastname");
		String email = request.getParameter("email");
		
		String password = request.getParameter("password");
		String confirmPassword = request.getParameter("confirm_password");
		String dob = request.getParameter("dob");
		String state = request.getParameter("state");
		if (!password.equals(confirmPassword)) {
			// throw error and respond with same register.jsp with values
		}
		@SuppressWarnings("deprecation")
		Volunteer user =new Volunteer(email,firstName, lastName,dob,state);
		user.setPassword(password);
		try {
			RegistrationService service = new RegistrationService();
			service.registerUser(user);
			HttpSession session = request.getSession();
			session.setAttribute("username", firstName );
			setMenuItems(session);
			RequestDispatcher dispatcher = getServletContext()
					.getRequestDispatcher("/home.jsp");
			dispatcher.forward(request, response);
		} catch (Exception e) {
			logger.error("Error while registration", e);
			// show error on page
			request.setAttribute("error", e.getMessage());
			request.setAttribute("states", MasterDataService.getStates());
			RequestDispatcher dispatcher = getServletContext()
					.getRequestDispatcher("/register.jsp");
			dispatcher.forward(request, response);

		}
		

	}
	private void setMenuItems(HttpSession session) {
		Map<String, String> roleBasedMenu = new LinkedHashMap<String,String>();
		roleBasedMenu.put("index.jsp","Home Page");
		roleBasedMenu.put("newCase", "Submit new case");
		roleBasedMenu.put("links.html", "Links");
		roleBasedMenu.put("connection.jsp", "volunteer");
		session.setAttribute("menu", roleBasedMenu);
	}
	

}
